package org.example.music_manage_system.service.lmpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.music_manage_system.mapper.MusicMapper;
import org.example.music_manage_system.pojo.Music;
import org.example.music_manage_system.pojo.PageBean;
import org.example.music_manage_system.service.MusicService;
import org.example.music_manage_system.utils.MusicChecker;
import org.example.music_manage_system.utils.ThreadLocalUtil;
//踩大坑，包调错了
/*import com.github.pagehelper.PageInfo;*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class MusicServiceImpl implements MusicService {

    //    新增曲目
    @Autowired
    private MusicMapper musicMapper;
    @Override
    public void add(Music music) {
//        同添加分类
        music.setCreateTime(LocalDateTime.now());
        music.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        music.setCreateUser(id);
        music.setPlayUrl(generatePlayUrl(music.getTitle()));
        boolean isFound = MusicChecker.checkMusic(music.getPlayUrl());
        music.setStates(isFound ? "已找到" : "未找到");
        musicMapper.add(music);
    }
    //查找音乐
    @Override
    public Music findById(Integer id) {
        Music music = musicMapper.findById(id);
        return music;
    }
    //修改音乐
    @Override
    public void update(Music music) {
        music.setUpdateTime(LocalDateTime.now());//修改时间
        music.setPlayUrl(generatePlayUrl(music.getTitle()));
        // 检查歌曲是否被找到
        boolean isFound = MusicChecker.checkMusic(music.getPlayUrl());
        music.setStates(isFound ? "已找到" : "未找到");
        musicMapper.update(music);

    }
    //删除音乐
    @Override
    public void delete(Integer id) {
        musicMapper.delete(id);
    }
    //分页显示
    @Override
    public PageBean<Music> list(Integer pageNum, Integer pageSize, Integer categoryId, String states) {
        //1.创建PageBean对象
        PageBean<Music> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Music> as = musicMapper.list(userId,categoryId,states);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<Music> p = (Page<Music>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
    @Override
    public String generatePlayUrl(String title) {
        // 将音乐标题转换为 URL 编码并生成播放 URL
        try {
            String encodedTitle = URLEncoder.encode(title, StandardCharsets.UTF_8.toString());
            return "http://search.5sing.kugou.com/?keyword=" + encodedTitle;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("音乐播放URL 编码失败", e);
        }
    }
}
